package com.zhang.crawler.movietv;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhang.crawler.utils.JdbcUtil;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhang sq
 * @date : 2020/7/29 17:25
 **/
public class DoubanCrawler {


    public static void main(String[] args) throws Exception {

        String parentUrl = "https://movie.douban.com/j/search_tags?type=tv&source=";
        String title = Jsoup.connect(parentUrl)
                .ignoreContentType(true).get().body().text();
        JSONArray tags = JSONArray.parseObject(title).getJSONArray("tags");
        Object[] obj = tags.toArray();
        for (int i = 0; i < obj.length; i++) {
            selectNode((String) obj[i]);
        }
    }

    private static void selectNode(String s) throws Exception {
        for (int i = 800; i < 10000; i=i+20) {
            //1.抓取信息，不是一个html网页，是一个Json字符串
            String url = "https://movie.douban.com/j/search_subjects?type=tv&tag=" + s + "&sort=recommend&page_limit=20&page_start=" + i;
            String json = Jsoup.connect(url)
                    .ignoreContentType(true).get().body().text();
            JSONArray subjects = JSONArray.parseObject(json).getJSONArray("subjects");
            if (subjects.isEmpty()) {
                break;
            }
            String s1 = String.valueOf(subjects.get(0));
            ParentEntity parentEntity1 = JSON.parseObject(s1, ParentEntity.class);
            String id = parentEntity1.getId();
            boolean query = JdbcUtil.query(id,JdbcUtil.getConn());
            if(!query){
                Thread.sleep(2000);
                System.out.println("电影已经存在了");
                continue;
            }
            List<DoubanEntity> entityList = new ArrayList<>();
            for (int j = 0; j < subjects.size(); j++) {
                String detail = String.valueOf(subjects.get(j));
                ParentEntity parentEntity = JSON.parseObject(detail, ParentEntity.class);
                DoubanEntity entity = selectDetail(parentEntity);
                entityList.add(entity);
            }
            JdbcUtil.insert(entityList);
            Thread.sleep(3000);
        }


    }

    //获取详情
    private static DoubanEntity selectDetail(ParentEntity parentEntity) throws IOException {
        DoubanEntity entity = new DoubanEntity();
        String id = parentEntity.getId();
        String detailUrl = "https://movie.douban.com/j/subject_abstract?subject_id=" + id;
        String js = Jsoup.connect(detailUrl)
                .ignoreContentType(true).get().body().text();
        System.out.println(id);
        String s = (String) JSONObject.parseObject(js).getString("subject");
        JSONObject json = JSON.parseObject(s);
        String episodes_count = json.getString("episodes_count");
        Double star = json.getDouble("star");
        String subtype = json.getString("subtype");
        String directors = json.getJSONArray("directors").toString();
        String actors = json.getJSONArray("actors").toString();
        String duration = json.getString("duration");
        String region = json.getString("region");
        String types = json.getJSONArray("types").toString();
        String release_year = json.getString("release_year");
        entity.setDoubanId(String.valueOf(id));
        entity.setTitle(parentEntity.getTitle());
        entity.setPlayAble(parentEntity.getPlayable());
        entity.setRate(parentEntity.getRate());
        entity.setUrl(parentEntity.getUrl());
        entity.setCover(parentEntity.getCover());
        entity.setIsNew(parentEntity.getIs_new());
        entity.setEpisodesCount(episodes_count);
        entity.setStar(star);
        entity.setSubType(subtype);
        entity.setDirectors(directors);
        entity.setActors(actors);
        entity.setDuration(duration);
        entity.setRegion(region);
        entity.setTypes(types);
        entity.setReleaseYear(release_year);
        return entity;
    }



}
